package com.orangelife.service.propFeeDirectBillLink.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeeDirectBillLink.IPropFeeDirectBillLinkService;
import com.orangelife.service.propFeeDirectBillLink.model.PropFeeDirectBillLinkDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.StringUtils;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_68001015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeeDirectBillLink类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-11-24 14:16:34
 * ==============================================================
 */
@Service("propFeeDirectBillLinkService")
public class PropFeeDirectBillLinkServiceImpl extends MybaitsGenericDaoSubLogic
		implements IPropFeeDirectBillLinkService {
	private static Logger logger = Logger
			.getLogger(PropFeeDirectBillLinkServiceImpl.class);

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PropFeeDirectBillLinkServiceImpl.logger = logger;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeeDirectBillLink(
			PropFeeDirectBillLinkDto propFeeDirectBillLink)
			throws AppBizException {

		Assert.notNull(propFeeDirectBillLink);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		if (!StringUtils.isBlank(propFeeDirectBillLink.getFeeDate())) {
			if (propFeeDirectBillLink.getFeeDate().length() > 10) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"收费期间如 2016-09长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeDirectBillLink.getRemark())) {
			if (propFeeDirectBillLink.getRemark().length() > 200) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "备注长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeDirectBillLink.getDescription())) {
			if (propFeeDirectBillLink.getDescription().length() > 200) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "描述长度超出");
			}
		}

		propFeeDirectBillLink.setDetailID(IdUtil.getuuId());
		propFeeDirectBillLink.setItemStat(TypeConst.FLAG_ON);
		propFeeDirectBillLink.setTscreate(new Date());
		propFeeDirectBillLink.setTsedit(new Date());
		return this.getSqlSession().insert(
				"PropFeeDirectBillLinkMapper.insertSelective",
				propFeeDirectBillLink);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPropFeeDirectBillLink(
			PropFeeDirectBillLinkDto propFeeDirectBillLink)
			throws AppBizException {

		Assert.notNull(propFeeDirectBillLink);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeDirectBillLink.getDetailID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeeDirectBillLinkDto oldPropFeeDirectBillLink = this
				.findPropFeeDirectBillLinkByID(propFeeDirectBillLink
						.getDetailID());

		if (!StringUtils.isBlank(propFeeDirectBillLink.getFeeDate())) {
			if (propFeeDirectBillLink.getFeeDate().length() > 10) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"收费期间如 2016-09长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeDirectBillLink.getRemark())) {
			if (propFeeDirectBillLink.getRemark().length() > 200) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "备注长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeDirectBillLink.getDescription())) {
			if (propFeeDirectBillLink.getDescription().length() > 200) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "描述长度超出");
			}
		}

		if (oldPropFeeDirectBillLink == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		propFeeDirectBillLink.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeDirectBillLinkMapper.updateByPrimaryKeySelective",
				propFeeDirectBillLink);
	}

	@Override
	public PropFeeDirectBillLinkDto findPropFeeDirectBillLinkByID(
			String propFeeDirectBillLinkID) throws AppBizException {
		if (StringUtils.isBlank(propFeeDirectBillLinkID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeDirectBillLinkDto oldPropFeeDirectBillLink = this
				.getSqlSession().selectOne(
						"PropFeeDirectBillLinkSearchMapper.selectByPrimaryKey",
						propFeeDirectBillLinkID);
		if (oldPropFeeDirectBillLink == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeeDirectBillLink.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeeDirectBillLink;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeeDirectBillLinkByLogic(String propFeeDirectBillLinkID)
			throws AppBizException {

		if (StringUtils.isBlank(propFeeDirectBillLinkID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeDirectBillLinkDto oldPropFeeDirectBillLink = this
				.findPropFeeDirectBillLinkByID(propFeeDirectBillLinkID);

		oldPropFeeDirectBillLink.setItemStat(TypeConst.FLAG_OFF);
		oldPropFeeDirectBillLink.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeDirectBillLinkMapper.updateByPrimaryKeySelective",
				oldPropFeeDirectBillLink);
	}

	@Override
	public List<Map<String, Object>> findPropFeeDirectBillLinkByPage(
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeDirectBillLinkSearchMapper.findAllPropFeeDirectBillLink",
						map);
		return list;
	}

	@Override
	public List<PropFeeDirectBillLinkDto> getPropFeeDirectBillLinkByPage(
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeeDirectBillLinkDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeDirectBillLinkSearchMapper.getAllPropFeeDirectBillLinkDto",
						map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeeDirectBillLinkByRelevantID(
			String detailID, String directBillID, String feeDetailID,
			String feeTypeID) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("detailID", detailID);
		map.put("directBillID", directBillID);
		map.put("feeDetailID", feeDetailID);
		map.put("feeTypeID", feeTypeID);

		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeDirectBillLinkSearchMapper.selectPropFeeDirectBillLinkDtoyRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeDirectBillLinkDto> findPropFeeDirectBillLinkByRelevantID(
			String detailID, String directBillID, String feeDetailID,
			String feeTypeID) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("detailID", detailID);
		map.put("directBillID", directBillID);
		map.put("feeDetailID", feeDetailID);
		map.put("feeTypeID", feeTypeID);

		List<PropFeeDirectBillLinkDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeDirectBillLinkSearchMapper.findPropFeeDirectBillLinkDtoByRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeDirectBillLinkDto> bauthfindPropFeeDirectBillLink(
			List<String> propFeeDirectBillLinkIDs) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeeDirectBillLinkDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeDirectBillLinkSearchMapper.batchFindPropFeeDirectBillLink",
						propFeeDirectBillLinkIDs);
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeeDirectBillLinkByMap(
			Map<String, Object> propFeeDirectBillLink) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeDirectBillLinkSearchMapper.batchFindPropFeeDirectBillLinkByMap",
						propFeeDirectBillLink);
		return list;
	}

	@Override
	public void clearCache() throws AppBizException {
		System.out.println("清除缓存成功");
	}

}